from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from cart import models, cartser
from utils.authentication import MyAuthentication


# Create your views here.
# 添加到购物车
class AddView(APIView):
    authentication_classes = [MyAuthentication]
    def post(self, request):
        data = models.CartModel.objects.filter(user=request.data.get('user'),course_id = request.data.get('course')).first()
        if data:
            return Response({"msg":"该商品已经购买过"})
        ser = cartser.CartSer(data = request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"添加成功"})
        return Response({"msg":ser.errors})
